package lib

import (
	"net"
	"strconv"
	"strings"
)

func Int64IPToStr(data int64) net.IP {
	b0 := byte(data >> 24 & 0xFF)
	b1 := byte(data >> 16 & 0xFF)
	b2 := byte(data >> 8 & 0xFF)
	b3 := byte(data & 0xFF)
	return net.IPv4(b0, b1, b2, b3)
}

func StrIPToInt64(ip string) int64 {
	datas := strings.Split(ip, ".")
	if len(datas) != 4 {
		return 0
	}

	var sum int64
	for k, _ := range datas {
		if b, _ := strconv.ParseInt(datas[k], 10, 64); b >= 0 && b < 256 {
			sum += b << uint((3-k)*8)
			continue
		}
		return 0
	}
	return sum
}
